﻿/** # component  : Carousel;
	# date       : 2012.02.17
	# author     : UI development team  part of lab.
**/

Fsz.Component.add("ContentRotator", Fsz.Class({
 /* Constructor*/
initialize : function (info){

	
	this.callbackArr = [];
	this._info = info;
	this._container = $(this._info.id);
	this._element = this.getElement(this._info.cls); /* item collect */	
	this.setButtons();
	this.speed = this._info.sec * 1000;
	this.timer = setTimeout(this.rotateContent.bind(this),this.speed);
	
	
},
/* get content-container */
getElement : function(cls){
	this._btns = [];
	var arr = this._container.$$("*"), i, ind=0;
	var item = [];
	for(i = 0; i<arr.length;i++){
		if(arr[i].className.indexOf(cls) > -1){
			item[ind] = new this.container(arr[i], ind);
			if(ind > 0){
				item[ind-1].next = item[ind];
				item[ind].prev = item[ind-1];
			}
			ind++;
		}
		if(arr[i].className.indexOf("prevBtn") > -1){
			this._btns[0] = arr[i];
		}
		if(arr[i].className.indexOf("nextBtn") > -1){
			this._btns[1] = arr[i];
		}
	}
	item[item.length-1].next = item[0];
	item[0].prev = item[item.length-1];
	item[0].display = true;
	return item;
},
/* setButtons */
setButtons : function(){
	if(this._btns.length !== 0){
		Fsz.Event.observe(this._btns[0],"click",this.leftContent.bind(this));
		Fsz.Event.observe(this._btns[1],"click",this.rightContent.bind(this));
	}
},
leftContent : function(){
	clearTimeout(this.timer);
	//if(this._element[0].display) return;
	var i, len = this._element.length;
	for(i = len-1; i >= 0; i--){
		if(this._element[i].display){
			this._element[i].display = false;
			this._element[i].prev.display = true;
			this._element[i].lender(this);
			this._element[i].prev.lender(this);
			i--;
		}
	}
	if(this._info.callback != undefined){
		this.callbackArr[this._info.callback]();
	}
	this.timer = setTimeout(this.rotateContent.bind(this),this.speed);
},
rightContent : function(){
	clearTimeout(this.timer);
	//if(this._element[this._element.length-1].display) return;
	var i, len = this._element.length;
	for(i = 0; i < len; i++){
		if(this._element[i].display){
			this._element[i].display = false;
			this._element[i].next.display = true;
			this._element[i].lender(this);
			this._element[i].next.lender(this);
			i++;
		}
	}
	this.timer = setTimeout(this.rotateContent.bind(this),this.speed);
},
rotateContent : function(){
	this.timer = setInterval(this.rightContent.bind(this),this.speed);
},
/* content-container object */
container : function(element, num){
	this.prev = null;
	this.next = null;
	this.element = element;
	this.index = num;
	this.display = false;
	
	this.lender = function(self){
		if(this.display){
			this.element.style.display = "block";
		}else{
			this.element.style.display = "none";
		}
		

	}
	
},

addCallBack : function(name,fc){
		this.callbackArr[name]  = fc;
}
}));